/*******************************************************************************
*    Stimulant or depressant? Resource-related income shocks and conflict      *
********************************************************************************


Author:   Kai Gehring, Sarah Langlotz, Stefan Kienberger
Email:    sarah.langlotz@uni-goettingen.de
Preamble: This dofile creates the figures and tables in the paper.
*******************************************************************************/

*------------------------------------------------------------------------------*
* Program Setup
*------------------------------------------------------------------------------*

drop _all
clear matrix
clear mata
set maxvar 10000
set matsize 10000

version 17.0

*------------------------------------------------------------------------------*

*------------------------------------------------------------------------------*
* Commands to be installed (uncomment in first run)
*------------------------------------------------------------------------------*

/*
cap ado uninstall estout
net install estout, from(http://fmwww.bc.edu/RePEc/bocode/e) //version 3.30  25mar2022
cap ado uninstall ftools
net install ftools, from(http://fmwww.bc.edu/RePEc/bocode/f) //version 2.37.0 16aug2019
cap ado uninstall reghdfe
net install reghdfe, from(http://fmwww.bc.edu/RePEc/bocode/r) //version 5.7.3 13nov2019
cap ado uninstall ivreg2
net install ivreg2, from(http://fmwww.bc.edu/RePEc/bocode/i) //version 4.1.11  22Nov2019
cap ado uninstall ivreghdfe
net install ivreghdfe, from(http://fmwww.bc.edu/RePEc/bocode/i) //version 1.0.0  07jul2018
cap ado uninstall ranktest
net install ranktest, from(http://fmwww.bc.edu/RePEc/bocode/r) //version 2.0.04  21sept2020
cap ado uninstall weakivtest
net install weakivtest, from(http://fmwww.bc.edu/repec/bocode/w) //version 10 28oct2020
cap ado uninstall avar
net install avar, from(http://fmwww.bc.edu/repec/bocode/a) //version 1.0.07  28July2015
cap ado uninstall coefplot
net install coefplot, from(http://fmwww.bc.edu/repec/bocode/c)
*/

*set font to Times New Roman
graph set window fontface "Times New Roman"


*------------------------------------------------------------------------------*
* Directories (set before first run, otherwise you receive an "invalid syntax" error)
*------------------------------------------------------------------------------*

local DIR = //Insert your directory here
cap cd "`DIR'"

**********************************************************************************************************************************************************************	
**********************************************************************************************************************************************************************
					**************************************  MAIN TEXT: Figures and Tables **************************************
**********************************************************************************************************************************************************************
**********************************************************************************************************************************************************************
clear
use "GLK_final_analysis.dta", clear
sort districtcode year

xtset



**************************Table 1: Main results, 2002-2014 period **************************
estimates drop _all
*Panel A: Local opium price
foreach Y in  lnbrd smallconflict lowconflict conflict war  { 	
	reghdfe `Y' llnwheat_rw_shock l.opiumshock_rw_lnopiumAFGnorm  suitability_rw_opium  if year>2001 , absorb(id province_id#i.year) cluster(districtcode) 
	est store fe3`Y'
}

esttab  fe3*  using "tables\normalizedlocaldrugprices2.tex", fragment booktabs se(3) b(3) nostar replace keep( L.opiumshock*) order(L.opiumshock*  ) varlabel(  L.opiumshock_rw_lnopiumAFGnorm "Opium Profitability (t-1)" ) mlabel(none)  style(fixed) nolines nogaps nonumbers postfoot( \hline) stats(r2_a , fmt(3) label( "Adjusted R-Squared")) noabbr		
estimates drop _all


*Panel B: International heroin price (Baseline)
foreach Y in  lnbrd smallconflict lowconflict conflict war  { 	
	reghdfe `Y' llnwheat_rw_shock l.opiumshock_rw_lnheroinnorm  suitability_rw_opium if year>2001 , absorb(id province_id#i.year  ) cluster(districtcode) 
	est store fe3`Y'
}

esttab  fe3*  using "tables\normalizedheroindrugprices2.tex", fragment booktabs se(3) b(3)   nostar  replace keep( L.opiumshock*) order(L.opiumshock*  ) varlabel( L.opiumshock_rw_lnheroinnorm "Opium Profitability (t-1)" ) mlabel(none)  style(fixed) nolines nogaps nonumbers postfoot( \hline) stats(r2_a , fmt(3) label( "Adjusted R-Squared")) noabbr 		
estimates drop _all		

*Panel C: Complement price
foreach Y in  lnbrd smallconflict lowconflict conflict war  { 	
	reghdfe `Y' llnwheat_rw_shock l.opiumshock_rw_lncomplementnorm  suitability_rw_opium  if year>2001 , absorb(id province_id#i.year) cluster(districtcode) 
	est store fe3`Y'
}

esttab  fe3*  using "tables\normalizedcomplementdrugprices2.tex", fragment booktabs se(3) b(3)   nostar  replace keep( L.opiumshock*) order(L.opiumshock*  ) varlabel( L.opiumshock_rw_lncomplementnorm "Opium Profitability (t-1)" ) mlabel(none) style(fixed) nolines nogaps nonumbers postfoot( \hline) stats(r2_a , fmt(3) label("Adjusted R-Squared")) noabbr //nonumbers 			
estimates drop _all	


**************************Table 2: Opportunity costs conditional on value added, 2002-2014 period**************************
cap estimates drop _all	

use "GLK_final_analysis.dta", clear
sort districtcode year

xtset

*Panel A	
foreach X in market total_markets anylab Ethnic_Connections { 
	replace opium_X= lopiumshock_rw_lnheroinnorm*`X'
	qui reghdfe lnbrd llnwheat_rw_shock lopiumshock_rw_lnheroinnorm opium_X c.llnheroinnormprice#c.`X'   `X'  if year>2001, absorb(id province_id#i.year) cluster(districtcode) 
	est store int2`X'
}	

esttab int2* using "tables\Table5_mechanisms2_PanelA.tex", fragment booktabs se(3) b(3)   nostar replace keep(lopiumshock_rw_lnheroinnorm opium_*) varlabel( lopiumshock_rw_lnheroinnorm "Opium Profitability (t-1)" opium_X "Opium Profitability (t-1)*X") mlabel(none) style(fixed) postfoot( \hline) noobs nonumbers nolines nogaps stats(r2_a , fmt(3) label("Adjusted R-Squared")) noabbr 

rename ma_totmarkets_3D ma_totmarkets_3d
estimates drop _all	
*Panel B
foreach X in ma_totmarkets_2d ma_totmarkets_3d ma_nightlight_2d ma_nighlight_3d { 
	replace opium_X= lopiumshock_rw_lnheroinnorm*`X'
	qui reghdfe lnbrd llnwheat_rw_shock lopiumshock_rw_lnheroinnorm opium_X c.llnheroinnormprice#c.`X'  `X'  if year>2001, absorb(id province_id#i.year) cluster(districtcode) 
	est store int2`X'
}	

esttab int2* using "tables\Table5_mechanisms2_PanelB.tex", fragment booktabs se(3) b(3)   nostar replace keep(lopiumshock_rw_lnheroinnorm opium_*) varlabel( lopiumshock_rw_lnheroinnorm "Opium Profitability (t-1)" opium_X "Opium Profitability (t-1)*X") mlabel(none) style(fixed) postfoot( \hline) noobs nonumbers nolines nogaps stats(r2_a , fmt(3) label("Adjusted R-Squared")) noabbr

estimates drop _all	


**************************Table 3: Government control and law enforcement, 2002-2014 period*************************

use "GLK_final_analysis.dta", clear
sort districtcode year

xtset

** US military 
forvalues x= 2(1)3{
	replace opium_X= lopiumshock_rw_lnheroinnorm*time_camp_`x'
	reghdfe lnbrd llnwheat_rw_shock lopiumshock_rw_lnheroinnorm opium_X (c.suitability_rw_opium   c.llnheroinnormprice)#c.time_camp_`x'  time_camp_`x' llnheroinnormprice if year>2001, absorb(id province_id#i.year) cluster(districtcode) 
	est store est_time_c`x'
}

** kabul time
foreach i of varlist totaltime_1 totaltime_2 totaltime_3 { 
	replace opium_X= lopiumshock_rw_lnheroinnorm*`i'
	reghdfe lnbrd llnwheat_rw_shock lopiumshock_rw_lnheroinnorm opium_X (c.suitability_rw_opium   c.llnheroinnormprice)#c.`i'  `i' llnheroinnormprice if year>2001, absorb(id province_id#i.year) cluster(districtcode) 
	est store est_tkab_`i'
}	


** anycity time
foreach j of varlist  totaltime_1_anycity totaltime_2_anycity totaltime_3_anycity { 
	replace opium_X= lopiumshock_rw_lnheroinnorm*`j'
	reghdfe lnbrd llnwheat_rw_shock lopiumshock_rw_lnheroinnorm opium_X c.llnheroinnormprice#c.`j'  `j' llnheroinnormprice if year>2001, absorb(id province_id#i.year) cluster(districtcode) 
	est store est_tcit`j'
}	


esttab est_time_c2 est_time_c3 est_tkab_totaltime_2  est_tkab_totaltime_3 est_tcittotaltime_2_anycity est_tcittotaltime_3_anycity using "tables\Mechanisms_camps_kab_any_time_2_3.tex", fragment booktabs se(3) b(3)   nostar replace keep(lopiumshock_rw_lnheroinnorm opium_*) varlabel( lopiumshock_rw_lnheroinnorm "Opium Profitability (t-1)" opium_X "Opium Profitability (t-1)*X") mlabel(none) style(fixed) postfoot( \hline) stats(r2_a , fmt(3) label("Adjusted R-Squared")) noobs nonumbers nolines nogaps noabbr		



************************* Figure 2 (b): Variation of international and local prices over time *************************
clear
use "GLK_final_analysis.dta"
sort districtcode year

xtset	

collapse (mean) heroinprice opiumAFGprice complementprice cocaineprice lncultivation cultivation, by (year)
label var complementprice "Int. Index Price"
label var heroinprice "Int. Heroin Price"
label var cocaineprice "Int. Cocaine Price"
label var opiumAFGprice "Afghan Opium Price"
label var cultivation "Opium Cultivation"
xtset, clear
tset year
gen fcultivation=f.cultivation
label var fcultivation "Opium Cultivation (t+1)"


graph twoway (line heroinprice year if year>=2001 & year<=2014 , legend(size(large)) lpattern(solid) color(black) yaxis(1) yscale(axis(1)) yscale(titlegap(2)) ytitle("International Prices", size(large) axis(1))  yscale(axis(1) range(20 120)) graphregion(color(white))  lwidth(medthick)) (line opiumAFGprice year if year>=2001 & year<=2014, legend(size(large)) lpattern(solid) color(gs12) graphregion(color(white))  yaxis(2) yscale(axis(2)) ytitle("Afghan Opium Price", size(large) axis(2))  yscale(axis(2) range(0 0.8)  titlegap(2)) lwidth(medthick)) (line complementprice year if year>=2001 & year<=2014, legend(size(large)) lpattern(dash) color(black) graphregion(color(white)) yaxis(1)  yscale(axis(1) range(20 120)) lwidth(medthick)) , title("International Heroin Price", color(black))

graph export "graphs\complement_prices.pdf", replace


*******************Figure 3: Effect of Opium Profitability (t-1) on standard of living indicators in (t)*******************
clear
use  "finaldata_hhlevel.dta", clear 
merge m:1 districtcode year using "GLK_final_analysis.dta" 
keep if _merge == 3
drop _merge

egen districtyear=group(districtcode year)

gen foodsecure=1-foodinsecure

label var dietarydiversity "Dietary Diversity"
label var calories_pm "Calorie Intake"
label var foodinsecure "Food Insecurity"
label var hhexp_total_2011 "Food Expenditures"
label var hhexp_total_paasche2011 "Food Exp. Paasche adj."
label var hhexp_total_lasp2011 "Food Exp. Laspeyres adj."
label var sumassets "Sum of Assets"
label var sumassets_weighted "Sum of Assets Weighted"
label var economicimprove "Economically Improved"

****for standardized outcomes
foreach X of varlist dietarydiversity calories_pm hhexp_total_2011 sumassets sumassets_weighted economicimprove hhexp_total_paasche2011 hhexp_total_lasp2011 {
	cap drop hhsample
	qui reghdfe `X' llnwheat_rw_shock lopiumshock_rw_lnheroinnorm  if year>2001  , absorb(id province_id#i.year) cluster(districtyear)
	gen hhsample=1 if e(sample)
	sum `X' if  hhsample==1 
	gen `X'sd=(`X'-r(mean))/r(sd)
	sum  `X'sd if  hhsample==1 
}


foreach X of varlist dietarydiversitysd calories_pmsd hhexp_total_2011sd {
	reghdfe `X' llnwheat_rw_shock lopiumshock_rw_lnheroinnorm  if year>2001  , absorb(id province_id#i.year) cluster(districtyear)
	est store est`X'
}

coefplot (estdietarydiversitysd, label(Dietary Diversity) ciopts(lcolor(gs14)) color(gs14) )  ///
	(estcalories_pmsd, label(Calorie Intake) ciopts(lcolor(gs10)) color(gs10) ) ///
	(esthhexp_total_2011sd, label(Food Expenditures) ciopts(lcolor(gs2)) color(gs2) ) ///
	, drop(_cons llnwheat_rw_shock) yline(0, lcolor(black)) vertical levels(90) graphregion(color(white)) legend(size(large)) ytitle(Coef. and 90% CI, color(black) size(large)) yscale(titlegap(2)) yscale(range(-0.4 0.8)) ylabel(-0.4 (0.1) 0.8)  title("")  xlabel("")

graph export "graphs\foodconsumption_sd.pdf", replace
est drop *

foreach X of varlist sumassetssd sumassets_weightedsd economicimprovesd {
	reghdfe `X' llnwheat_rw_shock lopiumshock_rw_lnheroinnorm  if year>2001  , absorb(id province_id#i.year) cluster(districtyear)
	est store est`X'
}


coefplot (estsumassetssd, label(Sum of Assets) ciopts(lcolor(gs12)) color(gs12) ) ///
	(estsumassets_weightedsd, label(Sum of Assets (weighted)) ciopts(lcolor(gs8)) color(gs8) ) ///
	(esteconomicimprovesd, label(Economically Improved) ciopts(lcolor(gs2)) color(gs2) ) ///
	, drop(_cons llnwheat_rw_shock) yline(0, lcolor(black)) vertical levels(90) graphregion(color(white)) legend(size(large)) ytitle(Coef. and 90% CI, color(black) size(large)) yscale(titlegap(2)) yscale(range(-0.4 0.8)) ylabel(-0.4 (0.1) 0.8)  title("")  xlabel("")
	
graph export "graphs\assets_sd.pdf", replace		
est drop *				


*******************Figure 5 (c): Marginal effect of Opium Profitability*******************

use "GLK_final_analysis.dta", clear

local i=0	
foreach talib in pashtun_ethno taliban1996 { 

	cap drop govc talc opium_govc opium_talc
	cap drop sd*
	gen govc = 0 
	replace govc = 1 if time_camp_2==1 
	gen talc = 0 
	replace talc = 1 if `talib'==1 & time_camp_2!=1

	local i=`i'+1

	gen opium_govc= lopiumshock_rw_lnheroinnorm*govc
	gen opium_talc = lopiumshock_rw_lnheroinnorm*talc

	reghdfe lnbrd llnwheat_rw_shock c.lopiumshock_rw_lnheroinnorm c.lopiumshock_rw_lnheroinnorm#i.(govc talc) c.llnheroinnormprice#c.talc c.llnheroinnormprice#c.govc   if year>2001, absorb(id province_id#i.year) cluster(districtcode) 
	est sto res_`talib'
	margins , dydx(lopiumshock_rw_lnheroinnorm) at(0.talc=1 0.govc=1) at(1.govc=1 0.talc=1)  at(1.talc=1 0.govc=1)  post
	estimates store mar_`i' 
	
}

set scheme s2color	
coefplot (mar_1, label(Pashtun Presence) ciopts(lcolor(gs10)) color(gs10) )  ///
		(mar_2, label(Former Territory) ciopts(lcolor(gs2)) color(gs2) ) ///
		, xline(0, lcolor(black))  levels(90) graphregion(color(white)) scheme(s2color) legend() xtitle(Coefficients and 90% CI, color(black)) xlabel(-3 (0.5) 3) xscale(titlegap(2)) coeflabels(1._at= "Contested"  2._at= "Government Control"  3._at= "Taliban Control" )
		
graph export "graphs\mixeddistricts_ethno.pdf", replace



	